﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace _443_Server
{
	public class Server
	{
		public static Player ActivePlayer { get; set; }

		public static void Main(string[] args)
		{
			Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
			listener.Bind(new IPEndPoint(IPAddress.Any, 43594));
			listener.Listen(10000);
			Console.WriteLine("Started.");

			while (true)
			{
				Socket accepted = listener.Accept();
				Console.WriteLine("Accepted connection.");
				HandleSocket(accepted);
				Thread.Sleep(500);
			}
		}

		public static void HandleSocket(Socket s)
		{
			NetworkStream nin = new NetworkStream(s);
			NetworkStream nout = new NetworkStream(s);
			int request = nin.ReadByte();
			if (request == 15)
			{
				Console.WriteLine("Update request from " + s.RemoteEndPoint.ToString() + ".");
				ClientRequest.ForfillRequest(nin, nout);
			}
			else
			{
				Console.WriteLine("Login request from " + s.RemoteEndPoint.ToString() + ".");
				ActivePlayer = new Player(s);
				ActivePlayer.Startup();
			}
		}
	}
}
